package com.xujp.consumerorder80.contorller;


import com.xujp.common.entity.CommonResult;
import com.xujp.common.entity.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author Xjp
 * @version 1.0
 * @date 2021/5/1 下午 03:19
 */
@RestController
@RequestMapping("/consumer")
@Slf4j
public class ConsumerOrderController {

//    public static final String PaymentSrv_URL = "http://localhost:18001";

    // 通过在eureka上注册过的微服务名称调用
    public static final String PaymentSrv_URL = "http://CLOUD-PAYMENT-SERVICE/";

    @Autowired
    private RestTemplate restTemplate;


    @PostMapping("/payment/insert")
    public CommonResult insert(Payment payment) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
        map.add("serial", payment.getSerial());

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String,
                String>>(map, headers);

        return restTemplate.postForObject(PaymentSrv_URL + "payment/insert", request,
                CommonResult.class);
    }

    @PostMapping("/payment/query")
    public CommonResult query() {
        return restTemplate.postForObject(PaymentSrv_URL + "payment/query", null,
                CommonResult.class);
    }

    @PostMapping("/payment/queryEntity")
    public CommonResult queryEntity() {
        ResponseEntity<CommonResult> entity = restTemplate.postForEntity(PaymentSrv_URL +
                "payment/query", null, CommonResult.class);

        log.info("entity Success");
        return entity.getBody();


    }

    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin()
    {
        String result = restTemplate.getForObject(PaymentSrv_URL+"/payment/zipkin/",
                String.class);
        return result;
    }



}
